import 'package:file_selector/file_selector.dart';
import 'package:flutter/material.dart';
import 'package:flutter_yfree/YFree.dart';

enum FileType { Image, Video }

class ChatFileButton extends StatefulWidget {
  const ChatFileButton({
    required this.fileType,
    required this.onSelectFiles,
    Key? key,
  }) : super(key: key);

  final FileType fileType;
  final Function(List<XFile> files) onSelectFiles;

  @override
  _ChatFileButtonState createState() => _ChatFileButtonState();
}

class _ChatFileButtonState extends State<ChatFileButton> {
  bool isShowPopup = false;

  @override
  Widget build(BuildContext context) {
    return YButton(
      isInk: true,
      child: Icon(widget.fileType == FileType.Image ? Icons.image_outlined : Icons.video_camera_back_outlined, color: Colors.grey.shade600, size: 25),
      onTap: () async {
        XTypeGroup xTypeGroup;
        switch (widget.fileType) {
          case FileType.Image:
            xTypeGroup = const XTypeGroup(label: 'images', extensions: ["jpg", "png", "gif"]);
            break;
          case FileType.Video:
            xTypeGroup = const XTypeGroup(label: 'videos', extensions: ["mp4"]);
            break;
        }
        widget.onSelectFiles(await openFiles(acceptedTypeGroups: [xTypeGroup]));
      },
    );
  }
}
